package com.qn.controller;

import com.qn.enums.ProcessInstanceStatusEnum;
import com.qn.result.BaseResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.warm.flow.core.dto.FlowParams;
import org.dromara.warm.flow.core.entity.Instance;
import org.dromara.warm.flow.core.orm.agent.WarmQuery;
import org.dromara.warm.flow.core.service.InsService;
import org.dromara.warm.flow.core.utils.page.Page;
import org.dromara.warm.flow.orm.entity.FlowInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProcessInstanceController {

    private final InsService insService;

    /**
     * 获取流程实例列表
     */
    @GetMapping("/instance/list")
    public ResponseEntity<BaseResponse<List<Map<String, Object>>>> getInstanceList(@RequestParam Long definitionId) {
        FlowInstance condition = new FlowInstance();
        condition.setDefinitionId(definitionId);

        Page<Instance> page = new WarmQuery<>(insService)
                .orderByCreateTime()
                .desc()
                .page(condition, new Page<>(1, 10));
        List<Instance> list = page.getList();

        List<Map<String, Object>> result = new ArrayList<>();
        if (!list.isEmpty()) {
            list.forEach(instance -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", instance.getId());
                map.put("definitionId", instance.getDefinitionId());
                map.put("name", instance.getFlowName());
                map.put("businessId", instance.getBusinessId());
                map.put("flowStatus", instance.getFlowStatus());
                result.add(map);
            });
        }
        return new ResponseEntity<>(new BaseResponse<>(result), HttpStatus.OK);
    }


    /**
     * 用户审批
     */
    private final String ASSIGNEE_USER_KEY = "assignee_user";
    /**
     * 专业负责人
     */
    private final String ASSIGNEE_MANAGER_KEY = "assignee_manager";
    /**
     * 业务副总
     */
    private final String ASSIGNEE_BUSSINESS_MANAGER_KEY = "assignee_bussiness_manager";
    /**
     * 行政副总
     */
    private final String ASSIGNEE_ADMIN_MANAGER_KEY = "assignee_admin_manager";
    /**
     * 总经理
     */
    private final String ASSIGNEE_GENERAL_MANAGER_KEY = "assignee_general_manager";

    /**
     * 启动流程.
     *
     * @return 流程key
     */
    @GetMapping("/start")
    public ResponseEntity<Object> startInstance(@RequestParam String flowCode) {
        //1.维护该流程的变量
        Map<String, Object> variables = new HashMap<>();
        variables.put(ASSIGNEE_USER_KEY, 1);
        variables.put(ASSIGNEE_MANAGER_KEY, 2);
        variables.put(ASSIGNEE_BUSSINESS_MANAGER_KEY, 3);
        variables.put(ASSIGNEE_ADMIN_MANAGER_KEY, 4);
        variables.put(ASSIGNEE_GENERAL_MANAGER_KEY, 5);

        String businessId = "1";
        FlowParams flowParams = FlowParams.build()
                .flowCode(flowCode)
                .variable(variables)
                .flowStatus(ProcessInstanceStatusEnum.RUNNING.name());

        Instance instance = insService.start(businessId, flowParams);

        //3.返回流程实例对象id
        return new ResponseEntity<>(new BaseResponse<>(instance.getId()), HttpStatus.OK);
    }

}
